package com.jy.servlet;

import com.google.gson.Gson;
import com.jy.dao.ManageDao;
import com.jy.dao.UserDao;
import com.jy.dao.impl.ManageDaoImpl;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.*;
import com.jy.util.CosFileupload;
import com.jy.util.JSONTool;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.bouncycastle.util.test.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@WebServlet("/man/*")
public class ManageServlet extends BaseServlet {
	ManageDao user=new ManageDaoImpl();
	Gson gson=new Gson();


	//登录
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String pass=request.getParameter("pass");
		System.out.println(name+"---"+pass);
		Admin a=null;
		try {
			a=user.login(name,pass);
			if(a==null){
				System.out.println("登录失败");
				response.sendRedirect("/examproject/nologin/Backgroundlogin.jsp");
			}else{
				System.out.println("登录成功");
				request.getSession().setAttribute("admin",name);
				response.sendRedirect("/examproject/manage/index.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//退出登陆
	protected void tuichu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().setAttribute("admin",null);
		response.sendRedirect("/examproject/nologin/Backgroundlogin.jsp");
	}
	//注册
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		String pass=request.getParameter("pass");
		System.out.println(name+"--"+phone+"--"+pass);
		Admin a=new Admin();
		a.setAname(name);
		a.setAphone(phone);
		a.setApass(pass);
		int n=0;
		System.out.println("getname:"+a.getAname().toString());
		try {
			List list=user.getyh(name);
			System.out.println("lsit:"+list);
			if(list.size()>0){
				response.sendRedirect("/examproject/nologin/forgotpassword.jsp?msg=true");
				System.out.println("用户存在");
			}else{
				n=user.add(a);
				if (n > 0) {
					System.out.println("注册成功");
					response.sendRedirect("/examproject/nologin/Backgroundlogin.jsp");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	//找回密码
	protected void pwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		System.out.println(name+"--"+phone);
		Admin a=null;
		try {
			a=user.pwd(name,phone);
			if(a==null){
				System.out.println("用户名或电话错误");
				response.sendRedirect("/examproject/nologin/getpass2.jsp");
			}else{
				System.out.println("成功");
				HttpSession session = request.getSession();
				session.setAttribute("name", name);
				response.sendRedirect("/examproject/nologin/newpwd2.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//修改密码
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pass=request.getParameter("pass");
		String name=request.getParameter("name");
		System.out.println(name+"---"+pass);
		Admin a=new Admin();
		a.setAname(name);
		a.setApass(pass);
		try {
			int b=user.update(a);
			System.out.println(b);
			if(b>0){
				System.out.println("修改成功");
				response.sendRedirect("/examproject/nologin/Backgroundlogin.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//查学生用户数
	protected void stucount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select count(stid) from student";
		try {
			int c=user.getstucount(sql);
			System.out.println("学生个数:"+c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//查老师用户数
	protected void teacount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select count(teacherId) from teacher";
		try {
			int c=user.getstucount(sql);
			System.out.println("老师个数:"+c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//查试卷数量
	protected void examcount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select count(exam_id) from exam";
		try {
			int c=user.getstucount(sql);
			System.out.println("试卷份数:"+c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//查考试记录试卷
	protected void exam(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql=" SELECT DISTINCT exam_name,exam_id FROM exam_record WHERE exam_zt='已交卷'";
		try {
			UserDao u=new UserDaoImpl();
			List list=u.getexamrecord(sql);
			String reg=gson.toJson(list);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//查单选题目数量
	protected void questiondancount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select count(question_id) from question where question_type=1";
		try {
			int c=user.getstucount(sql);
			System.out.println("单选题目数量:"+c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//查多选题目数量
	protected void questionduocount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select count(question_id) from question where question_type=2";
		try {
			int c=user.getstucount(sql);
			System.out.println("多选题目数量:"+c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	//后台作业
	protected void findhom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String ctime=request.getParameter("ctime");
			String bx=request.getParameter("bx");
			if(ctime==null){
				ctime="01";
			}
			System.out.println("bx就是"+bx);
			HttpSession session=request.getSession();
			System.out.println("ctime的长度"+ctime.length());
			System.out.println(ctime.toString().length()+"+++");
			UserDao user=new UserDaoImpl();
			try {
				if(ctime.length()>5){

					List c=(List) session.getAttribute("c");
					List list=user.addhom(ctime.substring(5,7));
					System.out.println(list);

					session.setAttribute("list",list);
					session.setAttribute("bx",bx);

					response.sendRedirect("/examproject/manage/zyqkht.jsp");
				}else{

					List list=user.addhom(ctime);
					System.out.println(list);
					session.setAttribute("list",list);
					session.setAttribute("bx",bx);
					response.sendRedirect("/examproject/manage/zyqkht.jsp");
				}
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
	}



	protected void tjhomht(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("textml");
		String ctime=request.getParameter("ctime");
		String stname=request.getParameter("stname");
		HttpSession session=request.getSession();
		try {
			List c=user.findstu2(stname);
			session.setAttribute("c",c);
			session.setAttribute("ctime",ctime);
			System.out.println(c);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		request.getRequestDispatcher("/man/findhom").forward(request,response);
	}

	protected void addidht(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session=request.getSession();
		String Stname=request.getParameter("stname");
		System.out.println("姓名:"+Stname);
		String checkIn=request.getParameter("checkIn");
		String work=request.getParameter("work");
		String ctime=request.getParameter("ctime");
		String remark=request.getParameter("remark");
		try {
			UserDao use=new UserDaoImpl();
			List<Object> list=new ArrayList<Object>(use.addid(Stname));
			System.out.println(list);
			System.out.println("list.Array().length的长度"+list.toArray().length);
			Object[] array = new Object[list.size()];
			if(list.toArray().length==0){
				System.out.println("进来了");
				request.getRequestDispatcher("/man/findhom?bx=false&ctime=01").forward(request,response);
			}
			System.out.println("可以运行到这里");
			array[0] = list.get(0);
			session.setAttribute("stname",Stname);
			session.setAttribute("checkIn",checkIn);
			session.setAttribute("work",work);
			session.setAttribute("ctime",ctime);
			session.setAttribute("remark",remark);
			session.setAttribute("array",array[0]);
			request.getRequestDispatcher("/man/tjzyht").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}

	}
	protected void tjzyht(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		HomeWork h=new HomeWork();
		HttpSession session=request.getSession();
		String checkIn=request.getParameter("checkIn");
		String work=request.getParameter("work");
		String ctime=request.getParameter("ctime");
		String remark=request.getParameter("remark");
		Object sid=session.getAttribute("array");

		System.out.println("这是array=="+sid);
		System.out.println("这是checkIn=="+checkIn);
		System.out.println("这是work=="+work);
		System.out.println("这是ctime=="+ctime);
		System.out.println("这是remark=="+remark);
		String sid1=sid.toString().substring(sid.toString().indexOf("=")+1,sid.toString().length());
		System.out.println("sid1=="+sid1);
		h.setStudentId(Integer.parseInt(sid1));
		h.setCheckIn(checkIn);
		h.setWork(work);
		h.setCtime(ctime);
		h.setRemark(remark);
		try {
			int c=user.tjzy(h);
			System.out.println(c);
			session.setAttribute("ctime",ctime);
			request.getRequestDispatcher("/man/tjhomht").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}

	}
	protected void delhomht(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		HomeWork h=new HomeWork();
		String id=request.getParameter("id");
		String ctime=request.getParameter("ctime");
		System.out.println(ctime);
		try {
			int c=user.delhom(Integer.parseInt(id));
			System.out.println(c);
			response.sendRedirect("findhom?ctime="+ctime);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}
	protected void cxsybjht/*查询所有班级*/(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		try {
			List listbj=user.cxsybj()/*查询所有班级*/;
			String reg=gson.toJson(listbj);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (Exception throwables) {
			throwables.printStackTrace();
		}
	}
	protected void cxsybj/*查询所有班级*/(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session=request.getSession();
			List list=user.cxsybj/*查询所有班级*/();
			System.out.println(list);
			session.setAttribute("bjlist",list);
			request.getRequestDispatcher("/man/cxsysj").forward(request,response);

		} catch (Exception throwables) {
			throwables.printStackTrace();
		}
	}

	protected void cxsysj/*查询所有试卷*/(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		try {

			List list=user.cxsysj/*查询所有试卷*/();
			System.out.println();
			System.out.println(list);
			List list1= (List) session.getAttribute("bjlist");
			session.setAttribute("list",list);
			session.setAttribute("bjlist",list1);
			response.sendRedirect("/examproject/manage/ksqk.jsp");
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}


	protected void cxksbj/*查询考试班级*/(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String classId=request.getParameter("classId");
		String p=request.getParameter("p");
		System.out.println(p);
		//PageBean pb=new PageBean();
		int intp=1;
		if(p!=null)
		{
			intp=Integer.parseInt(p);
		}
		try {
			int last=intp-1;
			int next=intp+1;
			if(last<1){
				last=1;
			}

			//String sql1="select count(*) from myksjl";
			String sql="select *from myksjl where classId="+classId;
			/*int count=user.getCount(sql1);
			pb.setTotal(count);//总页
			System.out.println(intp);
			pb.setCurPage(intp);//当前页*/
			List list=user.cxksbj/*班级查询考试*/(sql/*,pb.getCurPage(), 5*/);
			HttpSession session=request.getSession();
			//session.setAttribute("count", count);
			session.setAttribute("last", last);
			session.setAttribute("intp",intp);
			session.setAttribute("next", next);
			session.setAttribute("list1",list);
			System.out.println(session.getAttribute("list1"));
			System.out.println(request.getAttribute("list1"));
			//session.setAttribute("pb", pb);
			response.sendRedirect("/examproject/manage/ksqk.jsp");
		} catch (Exception throwables) {
			throwables.printStackTrace();
		}
	}

	protected void cxkssj/*试卷查询考试*/(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String exam_name=request.getParameter("exam_name");
		System.out.println(exam_name);
		try {
			List list=user.cxkssj/*试卷查询考试*/(exam_name);
			System.out.println(list);
			HttpSession session=request.getSession();
			session.setAttribute("list1",list);
			response.sendRedirect("/examproject/manage/ksqk.jsp");
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}
	protected void delsj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String exam_record_id=request.getParameter("exam_record_id");
		String classId=request.getParameter("classId");
		System.out.println(exam_record_id);
		System.out.println(classId);
		try {
			int c=user.delsj(Integer.parseInt(exam_record_id));
			System.out.println(c);
			response.sendRedirect("cxksbj?classId="+classId);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}

	protected void getPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p=request.getParameter("p");
		int intp=1;
		if(p!=null)
		{
			intp=Integer.parseInt(p);
		}
		try {
			int count=user.count();
			PageBean pb=new PageBean();
			pb.setTotal(count);//总页
			pb.setCurpage(intp);//当前页
			//记住页码
			HttpSession session=request.getSession();
			session.setAttribute("curpage", intp);
			List list = user.getPage(intp);
			request.setAttribute("tclist", list);
			request.setAttribute("pb", pb);
			request.getRequestDispatcher("/manage/admin.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//模糊查询name
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		try {
			List list=user.find(name);
			System.out.println(list);
			request.setAttribute("tclist",list);
			request.getRequestDispatcher("/manage/admin.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//模糊查询职业
	protected void find2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name2");
		try {
			List list=user.find2(name);
			System.out.println(list);
			request.setAttribute("tclist",list);
			request.getRequestDispatcher("/manage/admin.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//删除
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		System.out.println(name);
		Teacher t=new Teacher();
		t.setTname(name);
		try {
			int n=user.delete(t);
			if(n>0){
				System.out.println("删除成功");
				response.sendRedirect("/examproject/man/getPage");
			}else{
				System.out.println("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//添加
	protected void addtc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		String phone=request.getParameter("phone");
		String job=request.getParameter("job");
		Teacher t=new Teacher();
		t.setTname(name);
		t.setTpass(pwd);
		t.setTphone(phone);
		t.setTjob(job);
		try {
			List list=user.getls(name);

			if(list.size()>0){
				System.out.println("用户存在");
				response.sendRedirect("/examproject/man/getPage?msg=true");
			}else{
				if(phone.length()>11){
					System.out.println("电话号码位数不能超过11位");
					response.sendRedirect("/examproject/man/getPage?msg=false");
				}else{
					int n=user.add(t);
					if(n>0){
						System.out.println("添加成功");
						response.sendRedirect("/examproject/man/getPage");
					}else{
						System.out.println("添加失败");
						response.sendRedirect("/examproject/man/getPage");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//修改
	protected void updatetc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String tpass=request.getParameter("pwd");
		String tphone=request.getParameter("phone");
		String tjob=request.getParameter("job");
		System.out.println(id+"--"+name+"--"+tphone+"--"+tjob+"--"+name);
		Teacher t=new Teacher();
		t.setTeacherId(Integer.parseInt(id));
		t.setTpass(tpass);
		t.setTphone(tphone);
		t.setTjob(tjob);
		t.setTname(name);
		System.out.println(tphone.length());
		try {
			if(tphone.length()>11){
				System.out.println("电话号码不能超过11位");
				response.sendRedirect("/examproject/man/getPage?msg=false");
			}else{
				int n=user.update(t);
				if(n>0){
					System.out.println("修改成功");
					response.sendRedirect("/examproject/man/getPage");
				}else{
					System.out.println("修改失败");
					response.sendRedirect("/examproject/man/getPage");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 后台查询评论
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void queryJ2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");

		String page = request.getParameter("page");
		String limit = request.getParameter("limit");
		String seaname = request.getParameter("seaname");
		//System.out.println("---------------------------------:"+page+":"+limit+":"+seaname);
		if(seaname==null){
			seaname="";
		}
		int count = user.getcount(seaname);
		UserDao user1=new UserDaoImpl();
		List student = user1.getStudentList();  //得到所有学生
		List message = user.queryall(Integer.parseInt(page),Integer.parseInt(limit),seaname);
		String json = "";
		for(int i=0;i<message.size();i++){
			Comment comm = (Comment)message.get(i);
			for(int j=0;j<student.size();j++){
				Student stu = (Student) student.get(j);
				if(comm.getUid()==stu.getStid()){
					json+="{\"cid\":"+comm.getCid()+",\"uname\":\""+stu.getStname()+"\",\"info\":\""+comm.getContent()+"\",\"time\":\""+comm.getTime()+"\"},";
				}
			}
		}
		String tt = "";
		if(!(json.equals(""))){
			tt = json.substring(0,json.length()-1);
		}
		String data = "{ \"code\": 0, \"msg\": \"空空如也！\", \"count\": "+count+", \"data\": ["+tt+"]}";
		response.getWriter().write(data);
	}

	/**
	 * 删除评论
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delcomments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//接受前端ajax post请求数据
		InputStreamReader insr = new InputStreamReader(request.getInputStream(),"utf-8");
		String result = "";
		int respInt = insr.read();
		while(respInt!=-1) {
			result +=(char)respInt;
			respInt = insr.read();
		}

		ManageDao udao = new ManageDaoImpl();
		JSONObject jsonobj = JSONObject.fromObject(result);  //转json对象
		List id = jsonobj.getJSONArray("listId");
		String tt = "";
		for(int i=0;i<id.size();i++){
//            System.out.println(i+":"+id.get(i));
			tt+=id.get(i)+",";
		}
		String in = tt.substring(0,tt.length()-1);
//        System.out.println(in);
		int de = udao.delcomment(in);
		udao.delreqly(in);
		response.getWriter().print(de);
	}

	//新增考题
	public void insertquestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String xueke = request.getParameter("name");
		String neirong = request.getParameter("name1");
		String fenshu  = request.getParameter("name2");
		int  fenshu1 = Integer.valueOf(fenshu);
		System.out.println(fenshu1);
		System.out.println(neirong);
		String type = request.getParameter("sex");
		String tmA = request.getParameter("name3");
		String tmB = request.getParameter("name4");
		String tmC = request.getParameter("name5");
		String tmD = request.getParameter("name6");
		String answer = request.getParameter("name7");
		Question question = new Question();
		question.setQuestion_name(neirong);
		question.setQuestion_score(fenshu1);
		int type1 = Integer.valueOf(type);
		question.setQuestion_type(type1);
		question.setQuestion_creator(1);
		question.setQuestion_optiona(tmA);
		question.setQuestion_optionb(tmB);
		question.setQuestion_optionc(tmC);
		question.setQuestion_optiond(tmD);
		question.setQuestion_answer(answer);
		Date date = new Date();
		question.setUpdate_time(date);
		question.setQuestion_bookid(Integer.parseInt(xueke));
		System.out.println("添加的题目为:"+question);
		int n = user.addquestion(question);
		String sum;
		if(n>0){
			System.out.println("添加题目成功...");
			response.sendRedirect("/examproject/manage/tt.jsp?sum=true");
		}else{
			System.out.println("添加题目失败...");
			response.sendRedirect("/examproject/manage/tt.jsp?sum=false");
		}
	}

	//查询注册码
	protected void chazcm(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String gj=request.getParameter("gj");
		UserDao use=new UserDaoImpl();
		try {
			List list= Collections.singletonList(use.getzcm());
			System.out.println(list);
			request.setAttribute("list", list);
			//request.setAttribute("pb", pb);
			request.getRequestDispatcher("/manage/zcm.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//老师修改注册码
	protected void lszcmupdate(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String zcm=request.getParameter("zcm");
		//int id= Integer.parseInt(request.getParameter("1"));
		System.out.println(zcm);
		register obj=new register();
		obj.setZcm_tea(zcm);
		obj.setRid(1);
		System.out.println(obj);
		int i=user.lszcmpdate(obj);
		System.out.println(i);
		request.getRequestDispatcher("/man/chazcm").forward(request, response);


	}
	//学生修改注册码
	protected void xszcmupdate(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String zcm=request.getParameter("zcm");
		//int id= Integer.parseInt(request.getParameter("1"));
		System.out.println(zcm);
		register obj=new register();
		obj.setZcm_stu(zcm);
		obj.setRid(1);
		System.out.println(obj);
		int i=user.xszcmpdate(obj);
		System.out.println(i);
		request.getRequestDispatcher("/man/chazcm").forward(request, response);
	}
	//上传文件
	protected void pdfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//工厂
		DiskFileItemFactory factory=new DiskFileItemFactory();
		//Servlet 文件上传
		ServletFileUpload upload=new ServletFileUpload(factory);
		//文件名乱码
		upload.setHeaderEncoding("UTF-8");
		try {
			List<FileItem> list=upload.parseRequest(request);
			for(FileItem fi:list){
				//判断是否文件和文字
				if(fi.isFormField()){
					System.out.println("文件名："+fi.getFieldName()+" "+fi.getString("UTF-8"));
				}else{
					long ti=System.currentTimeMillis();
					int wz=fi.getName().lastIndexOf(".");
					String filna=fi.getName().substring(wz+1);
					System.out.println("后缀名："+filna);
					//图片名字设置时间戳
					String pna=ti+"."+filna;
					System.out.println("图片名："+pna);
					InputStream in=fi.getInputStream();
					CosFileupload.upfile(in,pna);
					request.setAttribute("filna",pna);
					request.getRequestDispatcher("/man/addquestions").forward(request,response);
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
	}


	//批量导入题目
	protected void addquestions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
		//得到文件的名字
		String fna= (String) request.getAttribute("filna");
		String path="https://sls-cloudfunction-ap-guangzhou-code-1301165591.cos.ap-guangzhou.myqcloud.com/"+fna;
		URL u = new URL(path);
		InputStream inpu=u.openStream();
		Workbook work= WorkbookFactory.create(inpu);
		Sheet sheet=work.getSheetAt(0);
		//得到行数
		int rows=sheet.getPhysicalNumberOfRows();
		Row row=sheet.getRow(0);
		//得到列数
		int cols=row.getPhysicalNumberOfCells();
		int n=0;
		System.out.println("行数："+rows+" "+"列数："+cols);
		for(int i=1;i<rows;i++){
			row=sheet.getRow(i);
			String name=row.getCell(1).toString();
			String set1=row.getCell(2).toString();
			String ag11=row.getCell(3).toString();
			String ag22=row.getCell(4).toString();
			String ag3=row.getCell(5).toString();
			String ag4=row.getCell(6).toString();
			String ag5=row.getCell(7).toString();
			String ag6=row.getCell(8).toString();
			String ag7=row.getCell(9).toString();
			String ag8=row.getCell(10).toString();
			String ag99=row.getCell(11).toString();
			int ag2=(int)Double.parseDouble(ag22);
			int ag1=(int)Double.parseDouble(ag11);
			int set=(int)Double.parseDouble(set1);
			int ag9=(int)Double.parseDouble(ag99);
			Question q1=new Question();
			q1.setQuestion_name(name);
			q1.setQuestion_score(set);
			q1.setQuestion_creator(ag1);
			q1.setQuestion_type(ag2);
			q1.setQuestion_optiona(ag3);
			q1.setQuestion_optionb(ag4);
			q1.setQuestion_optionc(ag5);
			q1.setQuestion_optiond(ag6);
			q1.setQuestion_answer(ag7);
			Date now=new Date();
			q1.setUpdate_time(now);
			q1.setQuestion_bookid(ag9);
			try {
				n=user.addquestion(q1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(n>0){
			CosFileupload.delfile(fna);
			response.sendRedirect("/examproject/manage/CloudReadExecl.jsp?ts=true");
		}else{
			CosFileupload.delfile(fna);
			response.sendRedirect("/examproject/manage/CloudReadExecl.jsp?ts=false");
		}
	}


	protected void stugetPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p=request.getParameter("p");
		int intp=1;
		if(p!=null)
		{
			intp=Integer.parseInt(p);
		}
		try {
			int count=user.stucount();
			PageBean pb=new PageBean();
			pb.setTotal(count);//总页
			pb.setCurpage(intp);//当前页
			//记住页码
			HttpSession session=request.getSession();
			session.setAttribute("curpage", intp);
			List list = user.getstuPage(intp);
			request.setAttribute("stclist", list);
			request.setAttribute("pb", pb);
			request.getRequestDispatcher("/manage/stuadmin.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//模糊查询name
	protected void stufind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		try {
			List list=user.findstu(name);
			System.out.println(list);
			request.setAttribute("stclist",list);
			request.getRequestDispatcher("/manage/stuadmin.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//删除
	protected void studelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		System.out.println(name);
		Student t=new Student();
		t.setStname(name);
		try {
			int n=user.deletestu(t);
			if(n>0){
				System.out.println("删除成功");
				response.sendRedirect("/examproject/man/stugetPage");
			}else{
				System.out.println("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	//添加
	protected void stuadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String classid=request.getParameter("classid");
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		String phone=request.getParameter("phone");
		String sex=request.getParameter("sex");
		String wx=request.getParameter("wx");
		System.out.println(classid+"--"+name+"--"+pwd+"--"+phone+"--"+sex+"--"+wx);
		Student t=new Student();
		t.setClassid(Integer.parseInt(classid));
		t.setStname(name);
		t.setStpass(pwd);
		t.setStphone(phone);
		t.setStsex(sex);
		t.setStwx(wx);
		try {
			List list=user.getstu(name);
			if(list.size()>0){
				System.out.println("用户存在");
				response.sendRedirect("/examproject/man/stugetPage?msg=true");
			}else{
				if(phone.length()>11){
					System.out.println("电话号码位数不能超过11位");
					response.sendRedirect("/schoolexam/man/stugetPage?msg=false");
				}else{
					int n=user.addstu(t);
					if(n>0){
						System.out.println("添加成功");
						response.sendRedirect("/examproject/man/stugetPage");
					}else{
						System.out.println("添加失败");
						response.sendRedirect("/examproject/man/stugetPage");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//修改
	protected void stuupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String stid=request.getParameter("stid");
		String classid=request.getParameter("classid");
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		String phone=request.getParameter("phone");
		String sex=request.getParameter("sex");
		String wx=request.getParameter("wx");
		System.out.println(classid+"--"+name+"--"+pwd+"--性别："+sex+"--电话："+phone+"--微信："+wx);
		Student t=new Student();
		t.setStid(Integer.parseInt(stid));
		t.setClassid(Integer.parseInt(classid));
		t.setStname(name);
		t.setStpass(pwd);
		t.setStphone(phone);
		t.setStsex(sex);
		t.setStwx(wx);
		try {
			List list=user.getstu(name);
			if(phone.length()>11){
				System.out.println("电话号码位数不能超过11位");
				response.sendRedirect("/examproject/man/stugetPage?msg=false");
			}else{
				int n=user.updatestu(t);
				if(n>0){
					System.out.println("修改成功");
					response.sendRedirect("/examproject/man/stugetPage");
				}else{
					System.out.println("修改失败");
					response.sendRedirect("/examproject/man/stugetPage");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 修改考试
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void examup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		String json="";
		String name=request.getParameter("name");
		String desc=request.getParameter("desc");
		String limi=request.getParameter("lim");
		int timi=Integer.parseInt(limi);
		String classid=request.getParameter("classid");
		int csid=Integer.parseInt(classid);
		String id=request.getParameter("id");
		int eid=Integer.parseInt(id);
		Timetest te=new Timetest();
		te.setExam_id(eid);
		te.setExam_classid(csid);
		te.setExam_time_limit(timi);
		te.setExam_name(name);
		te.setExam_desc(desc);
		int upex=user.upexam(te);
		if(upex>0){
			json="upasug";
			response.getWriter().write(json);
		}else{
			json="uploa";
			response.getWriter().write(json);
		}
	}
	//修改时段试卷
	protected void examup1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException {
		String json="";
		String name=request.getParameter("name");
		String desc=request.getParameter("desc");
		String limi=request.getParameter("lim");
		int timi=Integer.parseInt(limi);
		String starttime=request.getParameter("starttime");
		String endtime=request.getParameter("endtime");
		String id=request.getParameter("id");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date starttime1=sdf.parse(starttime);
		Date endtime1=sdf.parse(endtime);
		int eid=Integer.parseInt(id);
		Timetest te=new Timetest();
		te.setExam_id(eid);
		te.setExam_start_date(starttime1);
		te.setExam_time_limit(timi);
		te.setExam_end_date(endtime1);
		te.setExam_name(name);
		te.setExam_desc(desc);
		System.out.println("时段试卷:"+te);
		int upex=user.uptimeexam(te);
		if(upex>0){
			json="upasug";
			response.getWriter().write(json);
		}else{
			json="uploa";
			response.getWriter().write(json);
		}
	}

	//修改固定试卷
	protected void examup2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException {
		String json="";
		String name=request.getParameter("name");
		String desc=request.getParameter("desc");
		String limi=request.getParameter("lim");
		int timi=Integer.parseInt(limi);
		String id=request.getParameter("id");
		int eid=Integer.parseInt(id);
		Timetest te=new Timetest();
		te.setExam_id(eid);
		te.setExam_time_limit(timi);
		te.setExam_name(name);
		te.setExam_desc(desc);
		System.out.println("固定试卷:"+te);
		int upex=user.upgdexam(te);
		if(upex>0){
			json="upasug";
			response.getWriter().write(json);
		}else{
			json="uploa";
			response.getWriter().write(json);
		}
	}


}
